//
//  KeHuLeiXingShaiXuanView.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/5.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "KeHuLeiXingShaiXuanView.h"

@implementation KeHuLeiXingShaiXuanView{
    NSInteger currentIndex;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void)awakeFromNib{
    [super awakeFromNib];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"KeHuLeiXingShaiXuanTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeHuLeiXingShaiXuanTableViewCell"];
    
//    [_mainTableView addTarget:self action:@selector(click)];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)];
    tap.delegate = self;
    [_mainTableView addGestureRecognizer:tap];
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UITableView class]]) {
        return YES;
    }
    
    return  NO;
}

- (void)click{
    if(_manageClickBlock){
        _manageClickBlock(@"",@"");
    }
}

- (void)setType:(NSInteger)type{
    _type = type;
}

- (void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [_mainTableView reloadData];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KeHuLeiXingShaiXuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeHuLeiXingShaiXuanTableViewCell"];
    if(currentIndex == indexPath.row){
        cell.itemLabel.textColor = rgba(40, 120, 255, 1);
    }else{
        cell.itemLabel.textColor = rgba(90, 98, 112, 1);
    }
    NSDictionary *dataDic = _dataSource[indexPath.row];
    if(_type == 0){
        cell.itemLabel.text = GetHandleString(dataDic, @"name");
    }else if(_type == 1){
        cell.itemLabel.text = GetHandleString(dataDic, @"poolName");
    }else if(_type == 2){
        cell.itemLabel.text = GetHandleString(dataDic, @"name");
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    currentIndex = indexPath.row;
    [_mainTableView reloadData];
    NSDictionary *dataDic = _dataSource[indexPath.row];
    if(_manageClickBlock){
        if(_type == 0){
            _manageClickBlock(GetHandleString(dataDic, @"sceneId"),GetHandleString(dataDic, @"name"));
        }else if(_type == 1){
            _manageClickBlock(GetHandleString(dataDic, @"poolId"),GetHandleString(dataDic, @"poolName"));
        }else if(_type == 2){
            _manageClickBlock(GetHandleString(dataDic, @"sceneId"),GetHandleString(dataDic, @"name"));
        }
        
    }
}

@end
